package com.ist412.pocke;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ToggleButton;

/**
 * @author Rob C
 * @version 1.0
 */
public class SettingsActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Remove title & Make fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.activity_settings);
		
		Button btnSave			= (Button) findViewById(R.id.settings_btnSave);
		Button btnCancel		= (Button) findViewById(R.id.settings_btnCancel);
		ImageView imgBack		= (ImageView) findViewById(R.id.settings_imgBack);
		EditText edtMinute 		= (EditText) findViewById(R.id.settings_edtRestMin);
		EditText edtSecond 		= (EditText) findViewById(R.id.settings_edtRestSec);
		ToggleButton tglVibrate = (ToggleButton) findViewById(R.id.settings_tglVibrate);
		
		int min = 0, sec = 0, time = 0;
		
		time = MainActivity._restTimer;
		
		min = time / 60000;
		sec = (time % 60000) / 1000;
		
		edtMinute.setText(Integer.toString(min));
		edtSecond.setText(Integer.toString(sec));
		btnSave.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		imgBack.setOnClickListener(this);
		
		if (MainActivity._vibrateTime == 0) {
			tglVibrate.setChecked(false);
		} else {
			tglVibrate.setChecked(true);
		}
		
	} // end onCreate()
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.settings_imgBack:
				MainActivity.loadData();
				finish();
				break;
			case R.id.settings_btnSave:
				if (saveSettings()) {
					MainActivity.saveData();
					MainActivity.loadData();
					finish();
				}
				break;
			case R.id.settings_btnCancel:
				MainActivity.loadData();
				finish();
				break;
		}
	}
	
	
	private final boolean saveSettings() {
		int min, sec, time;
		
		EditText edtMinute 	= (EditText) findViewById(R.id.settings_edtRestMin);
		EditText edtSecond	= (EditText) findViewById(R.id.settings_edtRestSec);
		ToggleButton tglVibrate = (ToggleButton) findViewById(R.id.settings_tglVibrate);
		
		min = getIntIfEntered(edtMinute);
		sec = getIntIfEntered(edtSecond);
		
		time = (min * 60000) + (sec * 1000);
		
		if (time > 0) {
			MainActivity._restTimer = time;
		} else {
			return false;
		}
		
		if (tglVibrate.isChecked()) {
			MainActivity._vibrateTime = 200;
		} else {
			MainActivity._vibrateTime = 0;
		}
		
		return true;
	}
	
	/**
	 * Attempts to convert entered String into an int
	 * @param edt	EditText widget to use
	 * @return	int conversion of string if successful, 0 otherwise
	 */
	private final int getIntIfEntered(EditText edt) {
		int ret = 0;
		
		if (edt.getText().toString().length() > 0) {
			try {
				ret = Integer.parseInt(edt.getText().toString());
			} catch (Exception e) { return 0; }
		} else {
			ret = 0;
		}
		
		return ret;
	}
	
} // end class SettingsActivity